﻿using B.S.DataBase.MyProject.Domain.productionworkorder;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.SCWorkOrderCommand;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.SCWorkOrderCommandHandler
{
    /// <summary>
    /// 生产工单的查询命令处理方法
    /// </summary>
    public class SCWorkOrderQueryCommandHandler : IRequestHandler<SCWorkOrderQueryCommand, APIResult<List<SCWorkOrderDtos>>>
    {
        /// <summary>
        /// 基本信息仓储接口
        /// </summary>
        private readonly IBaseRepository<ProductionWorkOrderBasicInfoSheet> jRepo;
        /// <summary>
        /// 物品仓储接口
        /// </summary>
        private readonly IBaseRepository<ProductionItem> wRepo;
        /// <summary>
        /// 物品名称仓储接口
        /// </summary>
        private readonly IBaseRepository<item_classification_detail> wnRepo;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="jRepo"></param>
        /// <param name="wRepo"></param>
        /// <param name="wnRepo"></param>
        public SCWorkOrderQueryCommandHandler(IBaseRepository<ProductionWorkOrderBasicInfoSheet>jRepo,IBaseRepository<ProductionItem>wRepo,IBaseRepository<item_classification_detail>wnRepo)
        {
            this.jRepo = jRepo;
            this.wRepo = wRepo;
            this.wnRepo = wnRepo;
        }

        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<SCWorkOrderDtos>>> Handle(SCWorkOrderQueryCommand request, CancellationToken cancellationToken)
        {
            var list = from a in jRepo.GetAll().Result
                       join b in wRepo.GetAll().Result on a.ProductionItemId equals b.ProductionItemId
                       join c in wnRepo.GetAll().Result on b.itemName equals c.item_classification_detailID
                       select new SCWorkOrderDtos
                       {
                           WorkOrderId = a.WorkOrderId,
                           WorkOrderName = a.WorkOrderName,
                           WorkOrderNumber = a.WorkOrderNumber,
                           item_name = c.item_name,
                           PersoninCharge = a.PersoninCharge,
                           AffiliatedDepartment = a.AffiliatedDepartment,
                           plannedOutput = b.plannedOutput,
                           IsDel = a.IsDel
                       };
            if (!string.IsNullOrEmpty(request.WorkOrderName))
            {
                list = list.Where(x => x.WorkOrderName.Contains(request.WorkOrderName));
            }
            
            list = list.OrderByDescending(x => x.WorkOrderId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            APIResult<List<SCWorkOrderDtos>> result = new APIResult<List<SCWorkOrderDtos>>();
            result.Code = APIEnum.成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.data = list.ToList();
            result.Count = list.Count();
            return await Task.FromResult(result);



        }




    }


}
